const { Model, Schema } = require('../helps/model');
const { formatData } = require('../helps/tools');
// const { executor } = require('../helps/model');
// CREATE TABLE `sound_category_relation` (
//   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
//   `album_id` int(11) unsigned NOT NULL COMMENT '专辑id',
//   `category_id` int(11) unsigned NOT NULL COMMENT '分类id',
//   `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
//   `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
//   PRIMARY KEY (`id`),
//   KEY `album` (`album_id`) USING BTREE,
//   KEY `category` (`category_id`) USING BTREE
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='磨耳朵分类关系表';

const schema = new Schema({
  primary: 'id',
  create_time: true,
  update_time: true,
  soft_delete: true,
  auto_increment: true,
  fields: ['id', 'album_id', 'category_id'],
  default: {},
});

export const model = new Model('sound_category_relation', schema);

/**
 * 查找
 * @param id
 * @returns {*}
 */
export function get(id) {
  return model.findByKey(id);
}

/**
 * 查找列表
 * @param ids
 * @returns {*}
 */
export function getIds(ids) {
  return model.findByQuery({
    where: [
      ['id', 'in', ids],
    ],
  });
}

/**
 * 创建
 * @param data
 * @returns {*}
 */
export function add(data) {
  return model.insert(formatData(schema.baseFields(), data), null, {
    [schema.getSoftDelete()]: null,
  });
}

/**
 * 更新
 * @param id
 * @param data
 * @returns {*}
 */
export function put(id, data) {
  return model.updateByKey(id, formatData(schema.baseFields(), data));
}

/**
 * 删除
 * @param id
 * @returns {*}
 */
export function del(id) {
  return model.deleteByKey(id);
}

/**
 * 列表分页
 * @param page
 * @param number
 * @returns {*}
 */
export function search(where, page = 1, number = 50, order = 'id', direction = 'desc') {
  return model.findListAndCount(where, page, number, `\`${order}\` ${direction}`);
}

export function list(page, number, order, direction) {
  return search([], page, number, order, direction);
}

export function listByAlbum(album_id) {
  return search([
    ['album_id', '=', album_id],
  ]);
}
